//
// Created by ta&to on 2022/10/9.
//

#ifndef HTTPSERVER_HTTP_HANDLER_H
#define HTTPSERVER_HTTP_HANDLER_H
#include "hash_util.h"
#include "http_core_config.h"
#include "http_multi_io.h"
#include "http_request.h"
#define FD_UNUSE    0
#define FD_USE      1

typedef struct http_fd_s http_fd_t;
typedef struct http_handler_s http_handler_t;
typedef struct http_event_context_control_s http_event_context_control_t;
typedef struct http_inner_request_before_facade_s http_inner_request_before_facade_t;
typedef struct http_inner_request_after_facade_s http_inner_request_after_facade_t;
typedef void (*http_callback_func)(http_inner_request_after_facade_t *hirqaft);
struct http_fd_s{
    int fd;
    void *ptr;
    char *fdc;
    pthread_mutex_t lock;                       // 文件描述符锁 对文件描述符的读写操作都要加锁
    unsigned int state;
};

struct http_handler_s{
    http_callback_func func;
};
struct http_event_context_control_s {
    http_handler_t *func_handler;
};
typedef struct http_pool_s http_pool_t;
struct http_pool_s{
    pthread_mutex_t http_mut_lock;
    pthread_rwlock_t http_rw_lock;
};
struct http_inner_request_before_facade_s{
#ifdef SOCKET_SERVER_SELECT
    int n;
#endif
    int fd;
    http_fd_t *fds;
    struct http_socket_event_loop_s *hset;
};
struct http_inner_request_after_facade_s{
    http_fd_t *fds;
    struct http_socket_event_loop_s *hset;
};

#ifdef SOCKET_SERVER_SELECT
void __socket_r_callback(struct http_socket_event_loop_s *hset, int fd, int n);
#else
void __socket_r_callback(struct http_socket_event_loop_s *hset, int fd);
void __socket_w_callback(struct http_socket_event_loop_s *hset, int fd);
#endif
void close_http(struct http_socket_event_loop_s *hset, http_fd_t *fds);

void requst_control(http_inner_request_after_facade_t *hirqaft);


http_socket_event_handle_t *copy_func_handler(struct http_socket_event_loop_s *hset);
void free_func_handler(struct http_socket_event_loop_s *hset, http_fd_t *fds);

#endif //HTTPSERVER_HTTP_HANDLER_H
